package com.qf.java2106.huangshengrui.hotel.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.qf.java2106.huangshengrui.hotel.common.ResultVO;
import com.qf.java2106.huangshengrui.hotel.constant.MessageConstant;
import com.qf.java2106.huangshengrui.hotel.dao.IUserDao;
import com.qf.java2106.huangshengrui.hotel.factory.BeanFactory;
import com.qf.java2106.huangshengrui.hotel.pojos.User;
import com.qf.java2106.huangshengrui.hotel.service.IUserService;

import java.sql.SQLException;

/**
 * @author q
 * @version V1.0
 * @date 2021/10/28
 **/
public class UserServiceImpl implements IUserService {

    IUserDao userDao = (IUserDao) BeanFactory.getBean("userDao");

    @Override
    public ResultVO userLogin(User loginUser) {
        try {
            Integer isAdmin = userDao.userLogin(loginUser);
            if (null==isAdmin){
                return ResultVO.error("用户名或密码错误~");
            }
            if (!(loginUser.getIsAdmin()==isAdmin.intValue())){
                return ResultVO.error("您没有该系统权限，请切换系统登陆~");
            }
            return ResultVO.ok("登陆成功~",loginUser);
        } catch (SQLException e) {
            e.printStackTrace();
            return ResultVO.error(e.getMessage());
        }
    }

    @Override
    public ResultVO userCheck(String username) {
        try {
            if (StringUtils.isEmpty(username)){
                return ResultVO.error(MessageConstant.USERNAME_IS_NOT_NULL);
            }
            User user = userDao.userCheck(username);
            if (null == user){
                return ResultVO.error(MessageConstant.USERNAME_IS_NOT_EXISTS_LOGIN);
            } else {
                return ResultVO.ok(MessageConstant.USERNAME_IS_EXISTS_LOGIN,user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return ResultVO.error(e.getMessage());
        }
    }

    @Override
    public ResultVO userLogon(User logonUser) {
        try {
            return ResultVO.ok("注册成功,请登陆~",userDao.userLogon(logonUser));
        } catch (SQLException e) {
            e.printStackTrace();
            return ResultVO.error("注册失败~",e.getMessage());
        }
    }

    @Override
    public ResultVO userLogonCheck(String userName) {
        try {
            if (StringUtils.isEmpty(userName)){
                return ResultVO.error(MessageConstant.USERNAME_IS_NOT_NULL);
            }
            User user = userDao.userCheck(userName);
            if (null == user){
                return ResultVO.ok(MessageConstant.USERNAME_IS_NOT_EXISTS);
            } else {
                return ResultVO.error(MessageConstant.USERNAME_IS_EXISTS,user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return ResultVO.error(e.getMessage());
        }
    }
}
